function filterThisField(listingName, fieldName, value)
{
	listingParameters[listingName]['filter'] = fieldName;
	listingParameters[listingName]['filter_value'] = value;
	return updateTable(listingName, true);
}

function checkAllCB($table, $cb){
	var status = $cb.get(0).checked;
	$table.find('.multiple_action').each(function(){
		this.checked = status;
		if (status)
			$(this).parent().parent().addClass('selected');
		else
			$(this).parent().parent().removeClass('selected');
	});
}


function multiple_action($this, $table){
	var action = $this.val();
	
	if (action != 'nothing' && $table.find('.multiple_action:checked').length == 0)
	{
		alert('Impossible action, no file selected...');
		$this.val('nothing');
		return false;
	}
	
	if (action == 'nothing' || (in_array(multiple_action_to_confirm,action) && !confirm('Are you sure to do that ?')) )
	{
		$this.val('nothing');
		return false;
	}
	
	var param = 'do=multiple_action&step='+currentStepName+'&action=' + action;
	
	$table.find('.multiple_action:checked').each(function(){
			param += '&'+$(this).attr('name');
	});
	
	$.ajax({
		type: "GET",
		url: core_uri+'/action.php',
		data: param,
		success: function(msg){
			if (msg == "done")
			{
				updateTable(getListingName($(this)), true);
			}
			else alert("TECHNICAL ERROR ! " + msg);

		}
	});
	$this.val('nothing');
	return true;
}

function changeItemsPerPage(listingName, itemsPerPage)
{
	listingParameters[listingName]['item_per_page'] = itemsPerPage;
	return !updateTable(listingName, true);
}

function changePage(listingName, page)
{
	listingParameters[listingName]['page'] = page;
	return !updateTable(listingName, true);
}

function changeListingType(listingName, listingType)
{
	listingParameters[listingName]['listing_type'] = listingType;
	return !updateTable(listingName, true);
}

function clearThisFilter(listingName)
{
	$inputFilter = $('div.listing_block.JS_ATTR_'+listingName+' input.filter_value');
	listingParameters[listingName]['filter'] = '';
	listingParameters[listingName]['filter_value'] = '';
	$inputFilter.val('Filter...');
	return updateTable(listingName, true);
}

function addThSlider(listingName, fieldName, lowestValue, highestValue, float_precision)
{
	listingParameters[listingName]['filter_value_min'][fieldName] = lowestValue;
	listingParameters[listingName]['filter_value_max'][fieldName] = highestValue;
	//TODO replace abbr by JS_ATTR_ etc etc etc
	alert('TODO 5');
	$('div.listing_block.JS_ATTR_'+listingName+' th[abbr='+fieldName+'] div.ui-slider-1').slider({
		range: true,
		startValue:0,
		range:10,
		change: function(e,ui) {
			filterThisField(
				listingName,
				fieldName,
				listingParameters[listingName]['filter_value_min'][fieldName]
				+ '|'
				+ listingParameters[listingName]['filter_value_max'][fieldName]);
		},
		slide: function(e,ui) {
			if($(e.target).is('.first'))
				listingParameters[listingName]['filter_value_min'][fieldName] = roundNumber(lowestValue + (ui.value*.01*(highestValue-lowestValue)), float_precision);
			else
				listingParameters[listingName]['filter_value_max'][fieldName] = roundNumber(ui.value*.01*highestValue, float_precision);
				
			//TODO replace X2 abbr by JS_ATTR_ etc etc etc
			alert('TODO 6');
			$('div.listing_block.JS_ATTR_'+listingName+' th[abbr='+fieldName+'] .minimum_value').text(listingParameters[listingName]['filter_value_min'][fieldName]);
			$('div.listing_block.JS_ATTR_'+listingName+' th[abbr='+fieldName+'] .maximum_value').text(listingParameters[listingName]['filter_value_max'][fieldName]);
		}
	});
}

function addThPeriodDatePicker(listingName, fieldName, lowestValue, highestValue, float_precision){
	$(".startDate,.endDate").datepicker({     beforeShow: customRange,     showOn: "both",     buttonImage: "templates/images/calendar.gif",     buttonImageOnly: true });
}

function customRange(input) { 
	 return {minDate: (input.id == "endDate" ? $("#startDate").datepicker("getDate") : null), 
		  maxDate: (input.id == "startDate" ? $("#endDate").datepicker("getDate") : null)}; 
} 

function sortBy(listingName, fieldName, direction)
{
	alert(listingName);
	if(direction == undefined)
		if (listingParameters[listingName]['sort_by'] == fieldName)
			listingParameters[listingName]['sort_by_direction'] = listingParameters[listingName]['sort_by_direction'] == 'ASC' ? 'DESC' : 'ASC';
		else
			listingParameters[listingName]['sort_by_direction'] = 'ASC';
	else
		listingParameters[listingName]['sort_by_direction'] = direction;
	listingParameters[listingName]['sort_by'] = fieldName;
	return updateTable(listingName, true);
}

function updateTable(listingName, fullReloadField)
{
	var fullReload = 'false';
	if (fullReloadField != 'undefined' && fullReloadField == true)
		fullReload = 'true';
	var param =
	 'do=updateListing'
	+'&step='+currentStepName
	+'&subdir='+listingParameters[listingName]['subdir']
	+'&request_name='+listingParameters[listingName]['request_name']
	+'&page='+listingParameters[listingName]['page']
	+'&item_per_page='+listingParameters[listingName]['item_per_page']
	+'&listing_type='+listingParameters[listingName]['listing_type']
	+'&sort_by='+listingParameters[listingName]['sort_by']
	+'&sort_by_direction='+listingParameters[listingName]['sort_by_direction']
	+'&filter='+listingParameters[listingName]['filter']
	+'&filter_value='+listingParameters[listingName]['filter_value']
	+'&numberOfPagesNumbersToShow='+listingParameters[listingName]['numberOfPagesNumbersToShow']
	+'&full_reload='+fullReload;
	
	$.ajax({
		type: "GET",
		url: core_uri+'/action.php',
		data: param,
		success: function(html){
			//TODO : manage errors and warnings
			if (fullReload == 'true')
				$('div.listing_block.listing_block.JS_ATTR_'+listingName).replaceWith(html);
			else
			{
			var $listing_content = $('div.listing_block.JS_ATTR_'+listingName+' .listing_content');
			$listing_content.empty().append(html);
			}
			initListingEvents();
			rewrite_tr_alternate_class($('table.listing'));
		}
	});
	var check_all_cb = $('div.listing_block.JS_ATTR_'+listingName+' .multiple_action_all:checked');
	if (check_all_cb.length > 0)
		check_all_cb.get(0).checked = false;
	refreshSteps();
	return true;
}

function getListingName($listingChildElement)
{
	var stop = false;
	var $element = $listingChildElement;
	do
	{
		if($element.is('div.listing_block'))
			stop = true;
		else
		{
			$element = $element.parent();
			if ($element.is('body'))
				stop = true;
		}
	}
	while (!stop);
	return _getClassAttribute('JS_ATTR_', $element);
}
